package com.candy.updDemo;


import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.Scanner;

/**
 * @author donglin
 * @Title:
 * @类名: ClientThread
 * @date 2020/6/4
 */
public class ClientThread extends Thread{
    private DatagramSocket socket;

    public ClientThread() {
        super("小红：");
    }

    @Override
    public void run() {
        try {
            socket=new DatagramSocket();
            byte[] bt=new byte[1024];
            DatagramPacket sdp=new DatagramPacket(bt,bt.length);
            while (true){
                //////////////////////////////发送信息////////////////////////////////

                Scanner sc=new Scanner(System.in);
                String strClientMsg=Thread.currentThread().getName();
                strClientMsg+=sc.next();
                DatagramPacket cdg=new DatagramPacket(strClientMsg.getBytes(),0,strClientMsg.getBytes().length,
                     new InetSocketAddress("127.0.0.1",9997));
                System.out.println(strClientMsg);
                socket.send(cdg);
                ////////////////////////////////接收消息///////////////////////////////
             socket.receive(sdp);
             int len=sdp.getLength();
             String cSendSmg=new String(bt,0,len);
             System.out.println(cSendSmg);
         }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
